package org.lanqiao.lg.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.lanqiao.bean.Images;
import org.lanqiao.lg.dao.impl.ImagesDaoImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/ImgServlet")
@MultipartConfig
public class ImgServlet extends BaseServlet {
	Gson gson = new Gson();
	ImagesDaoImpl imgdao = new ImagesDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "upLoad":
				upLoad();
				break;
			case "search":
				searchImgName();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

// 通过图片id获取图片名字
	private void searchImgName() {
		Integer Iid = getInt("Imgid");
		System.out.println("Img:" + Iid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		String Iifno = imgdao.searchImgName(Iid);
		if (Iifno != null) {
			redata.setCode(2);
			redata.setMsg("selectPositionByCId 成功");
			redata.setData(Iifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(Iifno);
			return;
		}

	}

//上传图片
	private void upLoad() {

		UUID filename = UUID.randomUUID();
		String imageType = getString("imageType");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i = -1;
//			地址
		String Path = request.getServletContext().getRealPath("/img");
//			获取多个上传文件
		Collection<Part> parts;
		try {
			parts = request.getParts();
			for (Part part : parts) {
//				判断上传的文件是否是符合要求
				System.out.println(Path + "\\" + filename + "." + part.getContentType());
				System.out.println(
						"image/jpeg".equals(part.getContentType()) + "-" + "image/png".equals(part.getContentType()));
				if ("image/jpeg".equals(part.getContentType()) || "image/png".equals(part.getContentType())) {
					i = imgdao.upLoadImg(new Images(filename.toString(), imageType));
					if (i > 0) {
						part.write(Path + "/" + filename + ".jpg");
					} else {
						redata.setCode(3);
						redata.setMsg("服务器异常");
						redata.setData(i);
						return;
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		redata.setCode(2);
		redata.setMsg("upLoadimage 成功");
		redata.setData(i);// 返回图片id
		return;

	}
}
